package com.cf.super_hero;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.junit.jupiter.api.Test;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JWT {
    //       密钥名字
    private static String singKey="superHero";
    //    密钥有效期
    private static Long clockOr=3600*1000L;

    public static String getJWT(Map<String,Object> map){
        /*
        生成JwT令牌
         */
//
        String jwt= Jwts.builder().signWith(SignatureAlgorithm.HS256,singKey)
                .setClaims(map)
                .setExpiration(new Date(System.currentTimeMillis()+clockOr))
                .compact();
        return  jwt;
    }
    public static Claims parseJwt(String str){
        /*
        解析jwt令牌,如果令牌失效，则会报错!
         */
        Claims claims= Jwts.parser()
                .setSigningKey(singKey)
                .parseClaimsJws(str)
                .getBody();
        return claims;
    }
    @Test
    public void testGet(){
        Map<String,Object> map=new HashMap<>();
        map.put("陈锋","122135");
        System.out.println(JWT.getJWT(map));
    }
//    @Test
    public void testParse(){
        JWT.parseJwt("eyJhbGciOiJIUzI1NiJ9.eyLpmYjplIsiOiIxMjIxMzUiLCJleHAiOjE3MzA0NjU5Mjd9.pIR4ppxQjZkcjwZA80p0D92dHAt83WZxJayUZyGkyrE");
    }
}
